//
//  TTCategory2Cell.m
//  02-分类界面
//
//  Created by peironggao on 2017/10/28.
//  Copyright © 2017年 gaopeirong. All rights reserved.
//

#import "TTCategory2Cell.h"
#import "TTCactegoryModel.h"
#define CATEGORY1_DEFAULT_BG_COLOR [UIColor lightGrayColor]
#define CATEGORY1_SELECTED_BG_COLOR [UIColor whiteColor]
#define CELL_TITLE_FONT [UIFont systemFontOfSize:12]

#define CELL2_MARGIN_LEFT 25

#define BTN_MARGIN_VERTICAL 8
#define BTN_MARGIN_HORIZONAL 8

@interface TTCategory2Cell ()

@property (nonatomic, strong) NSMutableArray *btnPool;

@property (nonatomic, strong) NSMutableArray *usingPool;

@end

@implementation TTCategory2Cell

- (void)setModel:(TTCactegoryModel *)model
{
    _model = model;
    [self resetUsingBtn];
    
    for (NSInteger i= 0 ; i < model.subCates.count; i++)
    {
        TTCactegoryModel *item = model.subCates[i];
        UIButton *btn = [self dequeueBtn];
        btn.tag = i;
        [btn setTitle:item.name forState:UIControlStateNormal];
        [self.usingPool addObject:btn];
        [self fizeSizeBtn:btn];
        [self.contentView addSubview:btn];
    }
    
    [self caculateRowsAndColumn];
}

- (void)caculateRowsAndColumn
{
    CGFloat cellW = self.tableView.frame.size.width;
    cellW -= 2 * CELL2_MARGIN_LEFT;
    
    UIButton *preBtn = nil;
    for (UIButton *btn in self.usingPool)
    {
        
        if (preBtn == nil)
        {
            btn.frame = CGRectMake(CELL2_MARGIN_LEFT, BTN_MARGIN_VERTICAL, btn.frame.size.width, btn.frame.size.height);
            cellW -= btn.frame.size.width;
        }
        else
        {
            if (cellW - BTN_MARGIN_HORIZONAL - btn.frame.size.width > 0)
            {
                btn.frame = CGRectMake(CGRectGetMaxX(preBtn.frame) + BTN_MARGIN_HORIZONAL
                                       , preBtn.frame.origin.y, btn.frame.size.width, btn.frame.size.height);
                cellW -= (btn.frame.size.width + BTN_MARGIN_HORIZONAL);
            }
            else
            { // 另起一行
                cellW = self.tableView.frame.size.width - 2 * CELL2_MARGIN_LEFT;
                btn.frame = CGRectMake(CELL2_MARGIN_LEFT, CGRectGetMaxY(preBtn.frame) + BTN_MARGIN_VERTICAL, btn.frame.size.width, btn.frame.size.height);
                cellW -= btn.frame.size.width;
                
            }
        }
        preBtn = btn;
    }
    
    _model.cellHeight = CGRectGetMaxY(preBtn.frame) + BTN_MARGIN_VERTICAL;
    
}

- (void)fizeSizeBtn:(UIButton *)btn
{
    CGFloat innerMargin = 8;
    [btn sizeToFit];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = btn.frame.size.width + innerMargin * 2;
    CGFloat h = btn.frame.size.height + innerMargin * 2;
    btn.frame = CGRectMake(x, y, w, h);
}

- (void)btnDidClick:(UIButton *)btn
{
    if ([_delegate respondsToSelector:@selector(category2Cell:didClicked:)])
    {
        TTCactegoryModel *item = [_model.subCates objectAtIndex:btn.tag];
        [_delegate category2Cell:self didClicked:item];
    }
}

- (void)resetUsingBtn
{
    for (UIButton *item in self.usingPool)
    {
        [item removeFromSuperview];
        [self.btnPool addObject:item];
    }
    [self.usingPool removeAllObjects];

}

- (NSMutableArray *)btnPool
{
    if (!_btnPool)
    {
        _btnPool = [NSMutableArray array];
    }
    return _btnPool;
}

- (NSMutableArray *)usingPool
{
    if (!_usingPool)
    {
        _usingPool = [NSMutableArray array];
    }
    return _usingPool;
}

- (UIButton *)dequeueBtn
{
    UIButton *btn = [self.btnPool lastObject];
    if (btn)
    {
        [self.btnPool removeLastObject];
    }
    else
    {
        btn = [[UIButton alloc] init];
        btn.titleLabel.font = CELL_TITLE_FONT;
        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor blackColor].CGColor;
        btn.layer.borderWidth = 1;
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

@end
